//
//  SPUsersListViewController.m
//  Sport
//
//  Created by tikpen on 2018/4/29.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPUsersListViewController.h"
#import "SPConversationViewController.h"

@interface SPUsersListViewController ()<EaseUserCellDelegate,EMUserListViewControllerDataSource>
@property (nonatomic, strong) NSArray *usersList;
@end

@implementation SPUsersListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self contactsList];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)contactsList{
//    NSArray *userList = [[EMClient sharedClient].contactManager getContacts];
//    if (!userList) {
//
//    }
    [[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
        if (!aError) {
            NSLog(@"好友列表%@",aList);
            self.usersList = aList;
            [self.tableView reloadData];
        }else{
            NSLog(@"获取好友列表失败%@",aError);
        }
    }];
}

#pragma mark - UITableViewDataSource && UITableViewDelegate -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.usersList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    EaseUserCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[EaseUserCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    EMBuddy *buddy = self.usersList[indexPath.row];
    EaseUserModel *Model = [[EaseUserModel alloc]initWithBuddy:buddy];
    cell.model = Model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SPConversationViewController *conversationVC = [[SPConversationViewController alloc]initWithConversationChatter:self.usersList[indexPath.row] conversationType:EMConversationTypeChat];
    conversationVC.title = self.usersList[indexPath.row];
    [self.navigationController pushViewController:conversationVC animated:YES];
}

@end
